package mysql

import (
	"gorm.io/gorm"
	"taobao-merchandise/userrpc/userrpc"
)

type Goods struct {
	gorm.Model
	UserId    int64   `gorm:"type:int(11);not null;comment:用户id"`
	GoodsName string  `gorm:"type:varchar(20);not null;comment:商品名"`
	Price     float64 `gorm:"type:decimal(10,2);not null;comment:商品价格"`
	Stock     int64   `gorm:"type:int(11);not null;comment:商品库存"`
}

func (g *Goods) GoodsAdd(db *gorm.DB) error {
	return db.Debug().Create(g).Error
}

func (g *Goods) FindGoodsByName(db *gorm.DB, name string) error {
	return db.Debug().Where("goods_name=?", name).Find(g).Error
}

func (g *Goods) FindGoodsById(db *gorm.DB, id int64) error {
	return db.Debug().Where("id =? ", id).Find(g).Error
}

func (g *Goods) GoodsUpdate(db *gorm.DB) error {
	return db.Debug().Updates(g).Error
}

func (g *Goods) FindGoodsByGoodsIdAndUserId(db *gorm.DB, id int64, id2 int64) error {
	return db.Debug().Where("id =? and user_id =?", id, id2).First(g).Error
}

func (g *Goods) GoodsDel(db *gorm.DB, id int64) error {
	return db.Debug().Model(g).Where("id =?", id).Delete(g).Error
}
func GoodsList(db *gorm.DB, in *userrpc.GoodsListRequest) []*userrpc.GoodsList {
	var list []*userrpc.GoodsList
	db.Debug().Model(&Goods{}).
		Select(
			"goods.id goods_id",
			"goods_name",
			"price",
			"stock",
		).Scopes(Paginate(int(in.Page), int(in.Size))).
		Find(&list)
	return list
}
